﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DBDataContext db = LINQUtil.GetDBWriteOnly();
            var listProvince = (from pr in db.DefinitionPrices
                                join p in db.Locations on pr.LocationID equals p.ID
                                where pr.Status == 'A'
                                && p.ParentID == 0
                                && p.Status == 'A'
                                orderby p.LocationName
                                select new
                                {
                                    p.LocationName,
                                    p.ID
                                }).Distinct().ToList();

            if (listProvince.Count > 1)
            {
                foreach (var row in listProvince)
                {
                    ListItem ra = new ListItem();
                    ra.Text = row.LocationName;
                    ra.Value = row.ID.ToString();
                    cboProvince.Items.Add(ra);
                }
                cboProvince.SelectedIndex = 0;
            }

            else if (listProvince.Count == 1)
            {
                var pro = listProvince[0];
                Session["ProvinceID"] = pro.ID;
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                this.btnOK.Enabled = false;
            }
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string provinceID = cboProvince.SelectedValue;
        Session["ProvinceID"] = provinceID;
        Response.Redirect("~/Default.aspx");
    }
}